var TEMP_ID = null;
$(function(){
	$(".setting_form")
		.y_setting({
			ext_content : '.ext_content',
			ext_btn 	: '.add_sub_item',
			back_btn 	: '.back_btn',
			before_ext	: function(ext_btn, options){
				var title = $(ext_btn).parents('.item').find('.nav_item_title').val();
				if(title.length == 0){
					alert('请先设置一级导航标题！');
					return false;
				}
				return true;
			},
			on_ext 		: function(ext_btn, options){
				var title = $(ext_btn).parents('.item').find('.nav_item_title').val();
				var count = $(ext_btn).text();
				TEMP_ID = $(ext_btn).parents('.item').find('.temp_id').val();
				$(options.ext_content + '>' + options.back_btn).text(count);
				$(options.ext_content + '>h6').text(title + ' > 添加子导航');

				var items = $(ext_btn).next().children();
				$(options.ext_content + '>.ext_main_body').html(items);

				if(!$(options.ext_content + '>.ext_main_body>.nav_ul').length){
					var item = $('<ul class="nav_ul"><li class="item no_record"><a href="javascript:void(0)" class="img"><img src="/images/80x80/no_picture.jpg"></a>暂无记录！</li></ul>')
						.appendTo(options.ext_content + '>.ext_main_body');
					item.find('.img>img').auto_img();
				}

				$(options.ext_content + ' .add_sub_nav').unbind('click').click(function(){
					add_sub_item(options);
				});
			},
			before_back : function(ext_btn, prev_btn, options){
				if($(options.ext_content + '>.ext_main_body .error').length){
					alert('有错误，请先处理！');
					return;
				}
				return true;
			},
			on_back 	: function(ext_btn, prev_btn, options){
				$(options.ext_content + '>.ext_main_body .no_record').remove();
				var items = $(options.ext_content + '>.ext_main_body>.nav_ul');

				$(ext_btn).text(items.children().length).next().html(items);

				// console.log(ext_btn);
			}
		})
		.auto_check({
			'submit_btn' 	: '.save_btn',
			'next_focus'	: true
		})
		.ajax_form({
			'submit_btn' 	: '.save_btn',
			'form_position'	: null,
			'success' 		: function(form, data){
				window.location.href = '/manage/navigation/edit/' + data;
			}
		});

	//注册编辑器窗口自动适应窗口大小
	addWinResize(function(){
		$('.editor .edui-editor').width($('.setting_content').width() - 20);
	});

	$(document).on('click', '.del_link', function(){
		if(!confirm('是否继续删除？')){
			return;
		}
		var nav_ul = $(this).parents('.nav_ul:eq(0)');
		$(this).parents('.item').slideUp(function(){
			$(this).remove();
			var count = nav_ul.find('>li').length;
			if(!count){
				//无记录
				nav_ul.append('<li class="no_record"><img src="/images/80x80/no_picture.jpg"/>暂无记录！</li>');
				$('#Nav_title').set_focus();
			}else{
				nav_ul.find('input[type="text"]:eq(0)').set_focus();
			}
			var next_content = nav_ul.parents('#next_content'); 
			if(next_content.length){
				next_content.find('.back_btn').text(count);
			}
		});
	});

	$('.nav_ul .img>img').auto_img();

	$('.date').y_datepicker();

	$('#add_text_nav').click(function(){
		y_on_uploaded_nav($('.nav_ul'), null, 'pictures[]', '100x100')
	});

	$('.uploader').uploader({
		on_complete : function(holder_id, file){
			var image_path = '/uploads/temp/' + file.name;
			var img_content = $('#' + holder_id).parents('.img_content');
			img_content.find('.img>img').attr('src', '/uploads/temp/100x100/' + file.name).auto_img();
			img_content.find('.img>input').val(image_path);
		}
	});

	setTimeout(function(){
		// alert($('.add_sub_item:eq(0)').text());
		//$('.add_sub_item:eq(0)').click();

		setTimeout(function(){
			$('.add_sub_nav').click();
		}, 500);
	}, 200);
});

function add_sub_item(options){
	$('.no_record').remove();
	var temp_id = gen_id();
	var item = $('<li class="item">' + 
		'	<div class="img_content">' + 
		'		<div class="img">' + 
		'			<img src="/images/100x100/no_picture.jpg"/>' + 
		'			<input type="hidden" name="pictures[]" value=""/>' +
		'		</div>' + 
		'		<div class="uploader"></div>' +
		'	</div>' +
		'	<div class="item_info">' +
		'		<div class="row">' + 
		'			<strong>导航标题：</strong> ' + 
		'			<input type="text" class="nav_item_title" name="titles[]" rule="^.+$"/>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>导航链接：</strong> ' + 
		'			<input type="text" name="urls[]" />' +
		'			<p>点击导航后打开的网址</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>打开位置：</strong> ' + 
		'			<select name="targets[]">' + 
		'				<option value="_blank">新窗口</option>' + 
		'				<option value="_top">当前窗口</option>' + 
		'			</select>' +
		'			<p>打开网址的位置</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>有效期：</strong> ' + 
		'			<input type="text" name="starts[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/> - ' + 
		'			<input type="text" name="ends[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/>' +
		'			<p>空表示不限制</p>' +
		'		</div>' +
		'		<div class="row remark_row">' + 
		'			<strong>备注：</strong> ' + 
		'			<textarea name="descs[]"></textarea>' +
		'		</div>' +
		'	</div>' + 
		'	<div class="options">' +
		'		<a href="javascript:void(0)" class="del_link">删除</a>' +
		'	</div>' +
		'	<input type="hidden" name="parents[]" value="' + TEMP_ID + '"/>' +
		'	<input type="hidden" name="temp_ids[]" value="' + temp_id + '"/>' +
		'</li>').appendTo(options.ext_content + '>.ext_main_body>.nav_ul');
	
	item.auto_check({
		'submit_btn' : '.save_btn'
	});
	var item_count = $(options.ext_content + '>.ext_main_body>.nav_ul>li').length;
	$(options.ext_content + '>' + options.back_btn).text(item_count);
	item.find('img').auto_img();
	item.find('.date').y_datepicker();
	item.find('.uploader').uploader({
		on_complete : function(holder_id, file){
			var image_path = '/uploads/temp/' + file.name;
			var img_content = $('#' + holder_id).parents('.img_content');
			img_content.find('.img>img').attr('src', '/uploads/temp/100x100/' + file.name).auto_img();
			img_content.find('.img>input').val(image_path);
		}
	});
}

/**
 * 图片上传完成后执行
 */
function y_on_uploaded_nav(ul_id, file, name, thumb){
	$('.nav_ul>.no_record').remove();
	var image_path = null;
	var image_path_val = '';
	if(file === null){
		image_path = '/images/' + thumb + '/no_picture.jpg'
	}else{
		image_path = '/uploads/temp/' + thumb + '/' + file.name;
		image_path_val = '/uploads/temp/' + file.name;
	}
	var temp_id = gen_id();
	var li = $(
		'<li class="item">' + 
		'	<div class="img_content">' + 
		'		<div class="img">' + 
		'			<img src="' + image_path + '"/>' + 
		'			<input type="hidden" name="' + name + '" value="' + image_path_val + '"/>' +
		'		</div>' + 
		'		（点击修改图片）' +
		'	</div>' +
		'	<div class="item_info">' +
		'		<div class="row">' + 
		'			<strong>导航标题：</strong> ' + 
		'			<input type="text" class="nav_item_title" name="titles[]" rule="^.+$"/>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>导航链接：</strong> ' + 
		'			<input type="text" name="urls[]" />' +
		'			<p>点击导航后打开的网址</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>打开位置：</strong> ' + 
		'			<select name="targets[]">' + 
		'				<option value="_blank">新窗口</option>' + 
		'				<option value="_top">当前窗口</option>' + 
		'			</select>' +
		'			<p>打开网址的位置</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>有效期：</strong> ' + 
		'			<input type="text" name="starts[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/> - ' + 
		'			<input type="text" name="ends[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/>' +
		'			<p>空表示不限制</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>备注：</strong> ' + 
		'			<textarea name="descs[]"></textarea>' +
		'		</div>' +
		'	</div>' + 
		'	<div class="options">' +
		'		<a href="javascript:void(0)" class="del_link">删除</a>' +
		'	</div>' +
		'	<input type="hidden" name="parents[]" value=""/>' +
		'	<input type="hidden" name="temp_ids[]" value="' + temp_id + '" class="temp_id"/>' +
		'	<a href="javascript:void(0)" class="add_sub_item" title="添加子分类">0</a>' +
		'	<div class="sub_item"></div>' +
		'</li>'
	);
	$('.nav_ul').append(li);
	li.auto_check({
		'submit_btn' : '.save_btn'
	});
	li.find('img').auto_img();
	li.find('.date').y_datepicker();
}